<?php
/* SVN FILE: $Id: shop.php 222 2009-09-16 20:07:22Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Viewing the shop
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 222 $
* @lastrevision $Date: 2009-09-16 20:07:22 +0000 (Wed, 16 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-16 20:07:22 +0000 (Wed, 16 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_public/shop/shop.php $
*/

class public_ibmarket_shop_shop extends marketPublicCommand
{
	public static $orderBy = array(
									'ASC' => 1,
									'DESC' => -1,
								);
	public static $orderDefault = array(
										'item' => 'ASC',
										'icon' => 'ASC',
										'name' => 'ASC',
										'category' => 'ASC',
										'stock' => 'DESC',
										'resell' => 'DESC',
										'price' => 'ASC',
										'tax' => 'ASC',
										'expire' => 'DESC',
									);
	
	public function doExecute( ipsRegistry $registry )
	{
		$this->request['category'] = is_array($this->caches['market_category'][$this->request['category']]) ? $this->request['category'] : -1;
		$this->request['sub'] = isset($this->request['sub']) ? intval($this->request['sub']) : ($this->request['category'] == -1 ? 1 : 0);
		$this->request['order'] = isset(self::$orderDefault[$this->request['order']]) ? $this->request['order'] : 'stock';
		$this->request['by'] = isset(self::$orderBy[$this->request['by']]) ? $this->request['by'] : self::$orderDefault[$this->request['order']];
		
		$items = array();
		$categories = array($this->request['category']);
		if($this->request['sub'])
		{
			for($i = 0; $i < count($categories); $i++)
			{
				if(is_array($_categories = marketRegistry::getCategoryTree($categories[$i])))
				{
					foreach($_categories as $category)
					{
						$categories[] = $category['cat_id'];
					}
				}
			}
		}
		foreach($categories as $category)
		{
			if(!is_null($_items = marketRegistry::getItemsByCategory($category)))
			{
				foreach($_items as $item)
				{
					if($item['hide_item'] || in_array($this->memberData['member_group_id'],$item['limit_buy']))
					{
						continue;
					}
					$items[] = $item;
				}
			}
		}
		$subcategories = marketRegistry::getCategoryTree($this->request['category']);
		if(is_array($subcategories))
		{
			foreach($subcategories as $key => $category)
			{
				$subcategories[$key]['stats']['subcat'] = is_array(marketRegistry::getCategoryTree($category['cat_id'])) ? count(marketRegistry::getCategoryTree($category['cat_id'])) : 0;
				if(is_array($_items = marketRegistry::getItemsByCategory($category['cat_id'])))
				{
					$subcategories[$key]['stats']['items'] = count($_items);
					$subcategories[$key]['stats']['stock'] = 0;
					foreach($_items as $item)
					{
						if(numbers::compare($item['stock']) < 0)
						{
							$subcategories[$key]['stats']['stock'] = -1;
							break;
						}
						$subcategories[$key]['stats']['stock'] = numbers::add($subcategories[$key]['stats']['stock'], $item['stock']);
					}
				}
				else
				{
					$subcategories[$key]['stats']['items'] = 0;
					$subcategories[$key]['stats']['stock'] = 0;
				}
				$categories = array($category['cat_id']);
				for($i = 0; $i < count($categories); $i++)
				{
					if(is_array($_categories = marketRegistry::getCategoryTree($categories[$i])))
					{
						foreach($_categories as $category)
						{
							$categories[] = $category['cat_id'];
						}
					}
				}
				$subcategories[$key]['total']['subcat'] = count($categories) - 1;
				$subcategories[$key]['total']['items'] = 0;
				$subcategories[$key]['total']['stock'] = 0;
				foreach($categories as $cat_id)
				{
					if(is_array($_items = marketRegistry::getItemsByCategory($cat_id)))
					{
						$subcategories[$key]['total']['items'] = numbers::add($subcategories[$key]['total']['items'],count($_items));
						if(numbers::compare($subcategories[$key]['total']['stock']) >= 0)
						{
							foreach($_items as $item)
							{
								if(numbers::compare($item['stock']) < 0)
								{
									$subcategories[$key]['total']['stock'] = -1;
									break;
								}
								$subcategories[$key]['total']['stock'] = numbers::add($subcategories[$key]['total']['stock'], $item['stock']);
							}
						}
					}
				}
			}
		}
		else
		{
			$subcategories = array();
		}
		
		usort($items, array(self,'compareItemByInput'));
		
		if($this->request['category'] == -1)
		{
			$this->registry->getClass('output')->addNavigation( $this->lang->words['view_all_items'], "" );
		}
		else
		{
			$categories = array();
			$category = $this->request['category'];
			while($category != -1)
			{
				array_unshift($categories, $category);
				$category = $this->caches['market_category'][$category]['cat_parent_id'];
			}
			foreach($categories as $category)
			{
				$this->registry->getClass('output')->addNavigation( $this->caches['market_category'][$category]['cat_name'], "app=ibmarket&amp;category=".$category );
			}
		}
		
		self::$orderDefault[$this->request['order']] = self::$orderDefault[$this->request['order']] == 'ASC' ? 'DESC' : 'ASC';
		$content = $this->registry->getClass('output')->getTemplate('ibmarket')->shopArea($subcategories,$items,self::$orderDefault);
		
		$template = $this->registry->getClass('output')->getTemplate('ibmarket')->ibmarketHTMLWrapper($content);
		
		$this->registry->getClass('output')->setTitle( $this->settings['market_name'] );
		$this->registry->getClass('output')->addContent( $template );
		
        $this->registry->getClass('output')->sendOutput();
	}
	
	public static function compareItemByInput($a, $b)
	{
		$request =& ipsRegistry::fetchRequest();
		switch($request['order'])
		{
			case 'item':
				if($a['item_id'] < $b['item_id'])
				{
					return -1 * self::$orderBy[$request['by']];
				}
				elseif($a['item_id'] > $b['item_id'])
				{
					return 1 * self::$orderBy[$request['by']];
				}
				break;
			case 'tax':
				if($a['tax_amount'] < $b['tax_amount'])
				{
					return -1 * self::$orderBy[$request['by']];
				}
				elseif($a['tax_amount'] > $b['tax_amount'])
				{
					return 1 * self::$orderBy[$request['by']];
				}
				break;
			case 'resell':
			case 'price':
			case 'expire':
				if($a[$request['order']] < $b[$request['order']])
				{
					return -1 * self::$orderBy[$request['by']];
				}
				elseif($a[$request['order']] > $b[$request['order']])
				{
					return 1 * self::$orderBy[$request['by']];
				}
				break;
			case 'icon':
				if(($c = strcasecmp($a['icon'],$b['icon'])) != 0)
				{
					return $c * self::$orderBy[$request['by']];
				}
				break;
			case 'name':
				if(($c = strcasecmp($a['item_name'],$b['item_name'])) != 0)
				{
					return $c * self::$orderBy[$request['by']];
				}
				break;
			case 'category':
				$order = marketRegistry::getCategoryOrder();
				$order = array_flip($order);
				if($order[$a['category']] < $order[$b['category']])
				{
					return -1 * self::$orderBy[$request['by']];
				}
				elseif($order[$a['category']] > $order[$b['category']])
				{
					return 1 * self::$orderBy[$request['by']];
				}
				break;
			case 'stock':
				if(($c = numbers::compare($a['stock'],$b['stock'])) != 0)
				{
					if(numbers::compare($a['stock']) < 0)
					{
						return 1 * self::$orderBy[$request['by']];
					}
					if(numbers::compare($b['stock']) < 0)
					{
						return -1 * self::$orderBy[$request['by']];
					}
					return $c * self::$orderBy[$request['by']];
				}
				break;
		}
		return self::compareItemDefault($a, $b);
	}
	
	public static function compareItemDefault($a, $b)
	{
		if(($c = numbers::compare($a['stock'],$b['stock'])) != 0) // Higher stock first
		{
			if(numbers::compare($a['stock']) < 0)
			{
				return 1;
			}
			if(numbers::compare($b['stock']) < 0)
			{
				return -1;
			}
			return $c * self::$orderBy[$request['by']];
		}
		$ab = numbers::compare($a['price'],$b['price']); // Lower Price first
		if($ab > 0)
		{
			return 1;
		}
		elseif($ab < 0)
		{
			return -1;
		}
		$ab = numbers::compare($a['resell'],$b['resell']); // Higher resell first
		if($ab < 0)
		{
			return 1;
		}
		elseif($ab > 0)
		{
			return -1;
		}
		$ab = numbers::compare($a['total_tax'],$b['total_tax']); // Lower tax first
		if($ab > 0)
		{
			return 1;
		}
		elseif($ab < 0)
		{
			return -1;
		}
		if(($c = strcasecmp($a['item_name'],$b['item_name'])) != 0) // Smaller name first
		{
			return $c;
		}
		$order = marketRegistry::getCategoryOrder();
		$order = array_flip($order);
		if($order[$a['category']] < $order[$b['category']]) // Top category first
		{
			return 1;
		}
		elseif($order[$a['category']] > $order[$b['category']])
		{
			return -1;
		}
		if(($c = strcasecmp($a['icon'],$b['icon'])) != 0) // Smaller icon first
		{
			return $c;
		}
		if($a['item_id'] < $b['item_id']) // Smaller ID first
		{
			return -1;
		}
		elseif($a['item_id'] > $b['item_id'])
		{
			return 1;
		}
		return 0; // Should never right here
	}
}